package cn.alone.pet.service.impl;

import cn.alone.basic.service.impl.BaseServiceImpl;
import cn.alone.basic.util.CodeGenerateUtils;
import cn.alone.basic.util.DistanceUtil;
import cn.alone.basic.util.PageList;
import cn.alone.basic.util.Point;
import cn.alone.order.domain.PetAcquisitionOrder;
import cn.alone.order.mapper.PetAcquisitionOrderMapper;
import cn.alone.org.domain.Employee;
import cn.alone.org.domain.Shop;
import cn.alone.org.mapper.EmployeeMapper;
import cn.alone.org.mapper.ShopMapper;
import cn.alone.pet.domain.Pet;
import cn.alone.pet.domain.SearchMasterMsg;
import cn.alone.pet.mapper.SearchMasterMsgMapper;
import cn.alone.pet.query.SearchMasterMsgQuery;
import cn.alone.pet.service.IPetService;
import cn.alone.pet.service.ISearchMasterMsgService;
import cn.alone.user.domain.Logininfo;
import cn.alone.user.domain.User;
import cn.alone.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg masterMsg, Logininfo logininfo) {
        /*保存表单信息和查处用户店铺id,也要保存*/
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        System.out.println("----------------------------------"+logininfo);
        masterMsg.setUser_id(user.getId());
        /*使用经纬度工具类*/
        Point msgPoint = DistanceUtil.getPoint(masterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(msgPoint, shopMapper.queryAll());
        masterMsg.setShop_id(nearestShop.getId());
        searchMasterMsgMapper.save(masterMsg);
    }




    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Logininfo logininfo) {
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        if(user != null){
            query.setUserId(user.getId());
        }
        return super.querpage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(0);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.querpage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(1);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.querpage(query);
    }

    /**
     * 处理宠物信息
     * @param pet
     * @param
     */
    @Override
    public void handle(Pet pet, Logininfo logininfo) {
        /*根据id修改状态*/
        searchMasterMsgMapper.updateState2ProcessedById(pet.getSearch_master_msg_id());
        /*保存宠物信息和宠物详情*/
        petService.add(pet);
        /*创建收购订单*/
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, searchMasterMsg,employee.getId());
        petAcquisitionOrderMapper.save(acquisitionOrder);

    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }


}


